// index.js
// 获取应用实例
const $api = require("../../api/index").API
const $banner = require("../../api/banner").API

Page({
    data: {
        hei: null,
        CircleDynamicList:[],
        page:1,
        indicatorDots: true,
        vertical: false,
        autoplay: true,
        interval: 2000,
        duration: 500,
        bannerList:[],

    },
    // 事件处理函数
    bindViewTap() {

    },
    onLoad() {
        var t = this
        wx.getSystemInfo({
          success: (res) => {
              console.log(res)
              t.setData({
                windowWidth:res.windowWidth
              })
          },
        })
    },
    onReady(){
        var t = this
        t.getBannerByOwnerShip()
    },
    onShow(){
        var t = this
        var info = wx.getStorageSync('memberinfo')
        if(!info){
            wx.clearStorage()
            wx.redirectTo({
                url: '/pages/login/index'
            })
            return false
        }
        t.setData({
            page:1,
            info:wx.getStorageSync('memberinfo'),
            classinfo:wx.getStorageSync('TeacherClassinfo')
        })
        t.getCircleDynamicList()
    },
    add: function() {
        wx.navigateTo({
            url: '../index/index_add/index_add'
        })
    },
    onPullDownRefresh(){
        var t = this
        t.onShow()
    },
    onReachBottom(){
        var t = this
        t.setData({
            page:t.data.page +1 
        })
        t.getCircleDynamicList()

    },
    imgH(e) {
        var width = wx.getSystemInfoSync().windowWidth
            //获取可使用窗口宽度
        var imgheight = e.detail.height
            //获取图片实际高度
        var imgwidth = e.detail.width
            //获取图片实际宽度
        var height = width * imgheight / imgwidth
        this.setData({
            hei: height
        })
    },
    // 获取童圈列表
    getCircleDynamicList(){
        var t = this,
        data = {
            member_id:wx.getStorageSync('memberinfo').member_id,
            member_type:wx.getStorageSync('memberinfo').member_type,
            institution_id:wx.getStorageSync('memberinfo').institution_id,
            grade_id:"",
            class_id:"",
            pageNo:t.data.page,
            institution_longitude:0.1,
            institution_latitude:0.1
          }
        $api.getCircleDynamicList(data).then(res=>{
            console.log(res)
            var CircleDynamicList = t.data.CircleDynamicList
            CircleDynamicList = CircleDynamicList.concat(res.data)
            t.setData({
                CircleDynamicList:CircleDynamicList
            })
        })
    },
    // 图片预览
    showimg(e){
        console.log(e.currentTarget.dataset)
        var t = this
        let currentUrl = e.currentTarget.dataset.src
        let urllists = t.data.CircleDynamicList[e.currentTarget.dataset.index].circle_files
        var imglist = []
        for(let i in urllists){
            imglist.push(urllists[i].file_path)
        }
        wx.previewImage({
          current: currentUrl, // 当前显示图片的http链接
          urls: imglist // 需要预览的图片http链接列表
        })
    },
  // 根据位置获取不同的banner列表
    getBannerByOwnerShip(){
        var t = this,
        data = {
            banner_ownership:1
        }
        $banner.getBannerByOwnerShip(data).then(res=>{
            console.log(res)
            t.setData({
                interval:res.data.banner_refresh,
                bannerList:res.data.detail_list
            })
        })
    },
})